//日期
window.addEventListener('load', function () {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var dates = date.getDate();
    var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
    var day = document.querySelector('.header span');
    day.innerHTML = year + '-' + month + '-' + dates + '&nbsp;' + arr[date.getDay()];
})
//导航栏
window.addEventListener('load', function () {
    // var ul = document.querySelectorAll('.nav-box ul');
    var nav_box = document.querySelectorAll('.nav-box div');
    for (var i = 0; i < nav_box.length; i++) {
        nav_box[i + 1].onmouseover = function () {
            this.children[1].style.display = 'block';
            this.style.backgroundColor = '#142986';
        }
        nav_box[i + 1].onmouseout = function () {
            this.children[1].style.display = 'none';
            this.style.backgroundColor = '#1653b0';
        }
    }
})
//轮播图
window.addEventListener('load', function () {
    //获取元素
    var arrow_l = document.querySelector('.left');
    var arrow_r = document.querySelector('.right');
    var focus = document.querySelector('.focus');
    focus.addEventListener('mouseenter', function () {
        arrow_l.style.display = 'block';
        arrow_r.style.display = 'block';
    })
    focus.addEventListener('mouseleave', function () {
        arrow_l.style.display = 'none';
        arrow_r.style.display = 'none';
    })

    var ol = document.querySelector('.circle');
    var ul = focus.querySelector('ul');

    var focusWidth = focus.offsetWidth;
    for (var i = 0; i < ul.children.length; i++) {
        var li = document.createElement('li');
        li.setAttribute('index', i);
        ol.appendChild(li);
        li.addEventListener('click', function () {
            for (var i = 0; i < ol.children.length; i++) {
                ol.children[i].className = '';
            }
            this.className = 'current';
            var index = this.getAttribute('index');
            num = index;
            circle = index;

            animate(ul, -index * focusWidth);
        })
    }
    ol.children[0].className = 'current';
    var first = ul.children[0].cloneNode(true);
    ul.appendChild(first);
    var num = 0;
    var circle = 0;
    var flag = true;
    arrow_r.addEventListener('click', function () {
        if (flag === true) {
            flag = false;   //关闭节流阀
            if (num === ul.children.length - 1) {
                ul.style.left = 0 + 'px';
                num = 0;
            }
            num++;
            animate(ul, -num * focusWidth, function () {
                flag = true;    //打开节流阀
            });
            circle++;
            for (var i = 0; i < ol.children.length; i++) {
                ol.children[i].className = '';
            }
            if (circle == ol.children.length) {
                circle = 0;
            }
            ol.children[circle].className = 'current';
        }

    })
    arrow_l.addEventListener('click', function () {
        if (flag === true) {
            flag = false;
            if (num === 0) {
                num = ul.children.length - 1;
                ul.style.left = -num * focusWidth + 'px';
            }
            num--;
            animate(ul, -num * focusWidth, function () {
                flag = true;
            });
            circle--;
            for (var i = 0; i < ol.children.length; i++) {
                ol.children[i].className = '';
            }
            if (circle === ol.children.length) {
                circle = ol.children.length - 1;
            }
            ol.children[circle].className = 'current';
        }
    })
    var timer = setInterval(function () {
        arrow_r.click();
    }, 2000)
})
//更多
window.addEventListener('load', function () {

    var more = document.querySelectorAll('.box1 .t-right');
    for (var i = 0; i < more.length; i++) {
        more[i].addEventListener('mouseover', function () {
            this.innerHTML = 'more';
        })
        more[i].addEventListener('mouseout', function () {
            this.innerHTML = '更多';
        })
    }
})
//返回顶部
window.addEventListener('load',function (){
    function animate(obj, target, callback) {
        //如果给这个函数绑定一个button点击事件，当我们不断的点击按钮，这个元素的速度会越来越快，因为开启了太多定时器
        //解决方案就是 让我们元素只有一个定时器执行
        //先清除以前的定时器，只保留当前的一个定时器
        clearInterval(obj.timer);
        obj.timer = setInterval(function () {
            //把步长值改成整数
            var step = (target - window.pageYOffset) / 10;  //window.pageYOffset窗口移动的距离
            step = step > 0 ? Math.ceil(step) : Math.floor(step);
            if (window.pageYOffset === target) {
                //停止动画本质是停止定时器
                clearInterval(obj.timer);
                if (callback) {
                    //当动画停止时，判断是否存在callback函数，若有则调用。
                    callback();
                }
            } else {
                // obj.style.top = window.pageYOffset + step + 'px';
                window.scroll(0, window.pageYOffset + step);       //改成让窗口滚动
                //匀速动画就是 盒子的位置 等于当前位置+移动固定的值
                //缓动动画就是 盒子的位置 等于 当前的位置+变化的值 （目标值-现在的位置）/10）
            }
        }, 15);
    }
    var goBack=document.querySelector('.goBack');
    //3、当我们点击了返回顶部模块，就让窗口滚动到页面的最上方
    goBack.addEventListener('click', function () {
        // window.scroll(0,0); //滚动到页面(x,y)的距离。
        animate(window, 0);
    })
})

